<?php

namespace app\admin\controller;
use app\admin\model\FastCar as FastCarModel;
use my\Tools;
use think\Request;
use think\Db;
use app\admin\model\RegionModel;
use think\Cache;

class FastCar extends Base {

    protected $fast_car_model;

    public function _initialize() {
        parent::_initialize();
        $this->fast_car_model = new FastCarModel();
        $this->region_model         = new RegionModel();
        $provinceList = $this->region_model->getRegion(['parent_id'=>1,'region_level'=>1],'region_id,region_name');
        $this->assign('provinceList',$provinceList);
    }

    /**
     * 当日快车数据信息
     * @param string
     * @param int
     * @return string
     */
    public function index() {
        set_time_limit(0);
        $dateArray = input('post.');

        //$start_time = !empty($dateArray['start_time'])?strtotime($dateArray['start_time']):strtotime(date("Y-m-d"));
        //$end_time   = !empty($dateArray['end_time'])?strtotime($dateArray['end_time']):strtotime(date("Y-m-d 23:59:59"));
        //司机数据 - 在线人数/下线人数
        $driverInfo = $this->fast_car_model->totalDriverInfo($dateArray);
        $this->assign('driverInfo',$driverInfo);
        //订单数据
        $orderInfo  = $this->fast_car_model->totalOrderInfo($dateArray);
        $this->assign('orderInfo',$orderInfo);
        //流水数据
        $walletInfo = $this->fast_car_model->totalOrderMoney($dateArray);
        $this->assign('walletInfo',$walletInfo);
        //24小时订单数据量
        /*$dateArrOrder = [];
        while ($start_time <= $end_time){
            $dateArrOrder[] = date('Y-m-d',$start_time);
            $start_time = strtotime('+1 day',$start_time);
        }*/
        $dateArrOrder=date("Y-m-d");
        $sumOrderInfo = $this->fast_car_model->totalOrderNumber($dateArrOrder,$dateArray);
        //省份不为空
        $city_list = [];
        if(!empty($dateArray['province_id'])){
            $city_list = Db::name('region')->where(['parent_id'=>$dateArray['province_id']])->field('region_id,region_name')->select();
            $this->assign('city_list',$city_list);
        }
        $this->assign('sumOrderNumStr',$sumOrderInfo);
        $this->assign('dateArray',$dateArray);
        return $this->fetch();
    }
    //司机排行版
    public function driver(){
        $page = input('get.page');
        $limit = input('get.limit');
        if ($page && $limit) {
            $dateArray = input('get.');
            $list = $this->fast_car_model->getDriverList($page, $limit, $dateArray);
            echo json_encode($list);
            exit;
        }
        return $this->fetch();
    }
    /*
     * 司机时时位置数据
     * city_id 城市ID
    */
    public function translator(){
        $drivetType = input('post.drivetType');
        $highLight  = input('id');
        if(!empty($drivetType)){
            $data['driver_type']=$drivetType;
            $request                = Request::instance();
            $data['province_id']    = input('post.province_id')?input('post.province_id'):13;
            $data['city_id']        = input('post.city_id')?input('post.city_id'):133;
            $cityLocationKey        = 'cityLocationKey:'.$data['city_id'];
            $cityLocalInfo          = Cache::get($cityLocationKey);
            if(!empty($cityLocalInfo)){
                $location           = $cityLocalInfo;
            }else{
                $cityName           = Db::name('region')->where(['region_id'=>$data['city_id']])->value('region_name');
                $url                = "http://api.map.baidu.com/geocoder?address=".$cityName."&output=json&key=XgrsTKYvYuiq9wa2BUrfNXOW37E1DSdy&city=".$cityName;
                $result             = file_get_contents($url);
                $json               = json_decode($result,true);
                $location           = $json['result']['location'];
                Cache::set($cityLocationKey,$location);
            }
            $driverList             = $this->fast_car_model->driverListMongo($data);

            //开始处理司机 - 数据
            $driverArr = $driverLocationArr = [];
            foreach ($driverList as $key=>$value){
                $driverArr[]        = $value['driverId'];
                $driverLocationArr[$value['driverId']]['lng'] = $value['lng'];
                $driverLocationArr[$value['driverId']]['lat'] = $value['lat'];
            }
            //开始缓存司机信息
            $driverInfoKey          = 'driverlocation:info';
            $driverInfo             = Cache::get($driverInfoKey);//获取缓存中的司机信息
            if(empty($driverInfo)){                             //判断缓存司机信息是否存在
                $driverInfo = $driverList;
            }else{
                //循环缓存中的司机信息
                $driverArrCache = [];
                foreach ($driverInfo as $key=>$value){
                    if(in_array($value['driverId'],$driverArr)){//缓存的司机ID 是否在当前的司机信息 ID中 存在 说明司机在线
                        $driverInfo[$key]['driverType'] = 1;

                        //在判断该司机坐标是否发生变化 已发生变化需要重新赋值
                        if($value['lng'] == $driverLocationArr[$value['driverId']]['lng'] && $value['lat'] == $driverLocationArr[$value['driverId']]['lat']){
                            $driverInfo[$key]['locationStatus'] = 0;        //司机坐标未发生变化 不更新坐标
                            $driverInfo[$key]['highLight']=false;
                            if ($highLight){
                                $driverInfo[$key]['highLight']=true;
                            }

                        }else{
                            $driverInfo[$key]['locationStatus'] = 1;        //司机坐标发生变化 更新坐标
                            $driverInfo[$key]['lng']            = $driverLocationArr[$value['driverId']]['lng'];        //将新的坐标 替换
                            $driverInfo[$key]['lat']            = $driverLocationArr[$value['driverId']]['lat'];        //将新的坐标 替换

                            $driverInfo[$key]['highLight']=false;
                            if ($highLight){
                                $driverInfo[$key]['highLight']=true;
                            }
                        }
                    }else{
                        $driverInfo[$key]['driverType'] = 0;                //不存在 说明该司机已经下线

                    }
                    $driverArrCache[] = $value['driverId'];
                }
                //反过来 将 当前司机信息 与 缓存司机信息进行比较
                foreach ($driverList as $key=>$value){
                    if(!in_array($value['driverId'],$driverArrCache)){      //司机id是否存在 - 存在
                        $value['driverType']        = 1;
                        $value['locationStatus']    = 2;                    //新增司机
                        $driverInfo[] = $value;
                    }
                }
            }
            Cache::set($driverInfoKey,$driverList);
            $count      = count($driverList);
            $data = [
                'provinces' => $driverInfo,
                'url'       => $request->domain(),
                'count'     => $count,
                'location'  => $location,
            ];
            echo json_encode($data);die;
        }
        return $this->fetch('',['highLight'=>$highLight]);
    }
    //司机信息
    public function driverList(){
        //查询获取省份
        $page = input('get.page');
        $limit = input('get.limit');
        $data = input('get.');
        $data['driver_type']=1;
        $list = $this->fast_car_model->driverList($page,$limit,$data);
        echo json_encode($list);exit;
    }
    public function sendMsg(){
        Tools::sendMes('18555119143','2019/10/11 17:10:59',43);
    }



}
